package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/*jdbc:java数据库连接
步骤一：加载jdbc驱动程序
步骤二：建立与数据库的连接，并得到连接对象Connection
*/
public class DataBase {
	private final static String driver = "oracle.jdbc.driver.OracleDriver";
	private final static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	private final static String username = "scott";
	private final static String password = "tiger";
	protected Connection con = null;
	public void getcon(){
		
		
			try {
				Context ct = new InitialContext();
				DataSource ds=(DataSource)ct.lookup("java:comp/env/jdbc/yd");
				con=ds.getConnection();
				System.out.println(con);
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		
		/*try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
			System.out.println(con);
			
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	public static void main(String[] args) {
		DataBase db = new DataBase();
		db.getcon();
	}

}
	
